/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package name.coolwind.cxblog.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import name.coolwind.cxblog.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

/**
 *
 * @author coolwind
 */

@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    /**
     *
     * @param userEmail
     * @return the infomation of user include user_name and user_id
     */
    public User getUserByUserEmail(final String userEmail){
        String sqlStr = "select * from cx_user where user_email = ?";
        final User user = new User();
        this.jdbcTemplate.query(sqlStr, new Object[]{userEmail}, new RowCallbackHandler(){

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setUserEmail(userEmail);
            }
            
        });
        return user;
    }
    
    /**
     *
     * @param userId
     * @return
     */
    public User getUserByUserId(final int userId){
        String sqlStr = "select * from cx_user where user_id = ?";
        final User user = new User();
        this.jdbcTemplate.query(sqlStr, new Object[]{userId}, new RowCallbackHandler() {

            @Override
            public void processRow(ResultSet rs) throws SQLException {
                user.setUserId(userId);
                user.setUserName(rs.getString("user_name"));
            }
        });
        
        return user;
    }
    
    /**
     *
     * @param userEmail
     * @param password
     * @return check the user_name and password 
     */
    public boolean hasMatchUser(String userEmail, String password){
        String strSql = "select count(*) from cx_user where user_email = ? and password = ?";
        int num = this.jdbcTemplate.queryForObject(strSql, Integer.class, userEmail,password);
        return num > 0;
    }
    
    /**
     *
     * @param userName
     * @return if the user_name exists, it will be true, otherwise will be false
     */
    public boolean hasUserName(String userName){
        String strSql = "select count(*) from cx_user where user_name = ?";
        int num = this.jdbcTemplate.queryForObject(strSql, Integer.class, userName);
        return num > 0;
    }
    
    /**
     *
     * @param userEmail
     * @return if the user_email exists, it will be true, otherwise will be false
     */
    public boolean hasUserEmail(String userEmail){
        String strSql = "select count(*) from cx_user where user_email = ?";
        int num = this.jdbcTemplate.queryForObject(strSql, Integer.class, userEmail);
        return num > 0;
    }
}
